package org.budo.dubbo.protocol.http.exception;

import java.lang.management.ManagementFactory;

import org.budo.support.java.net.util.JavaNetUtil;
import org.budo.support.lang.util.StringUtil;

import lombok.Setter;

/**
 * @author lmw
 */
@Setter
public class BudoRuntimeException extends RuntimeException {
    private static final long serialVersionUID = 3820002119263843297L;

    private String message;

    public BudoRuntimeException() {}

    public BudoRuntimeException(String message) {
        super(message + runtimeInfo());
        this.setMessage(message + runtimeInfo());
    }

    public BudoRuntimeException(String message, Throwable cause) {
        super(message, cause);

        this.setMessage(message);
    }

    public BudoRuntimeException(Throwable cause) {
        super(cause);
    }

    @Override
    public String getMessage() {
        String supperMessage = super.getMessage();
        if (null != supperMessage && !supperMessage.isEmpty() && !"null".equals(supperMessage)) {
            return supperMessage;
        }

        return this.message;
    }

    private static String runtimeInfo() {
        return ", processName=" + ManagementFactory.getRuntimeMXBean().getName() //
                + ", ips=" + StringUtil.join(JavaNetUtil.getIpAddresses()) //
                + ", java.home=" + System.getProperty("java.home") //
                + ", java.io.tmpdir=" + System.getProperty("java.io.tmpdir");
    }
}